"""
https://leetcode.cn/problems/contains-duplicate-iii/description/

220. 存在重复元素 III
困难
相关标签
相关企业
提示
给你一个整数数组 nums 和两个整数 indexDiff 和 valueDiff 。

找出满足下述条件的下标对 (i, j)：

i != j,
abs(i - j) <= indexDiff
abs(nums[i] - nums[j]) <= valueDiff
如果存在，返回 true ；否则，返回 false 。

 

示例 1：

输入：nums = [1,2,3,1], indexDiff = 3, valueDiff = 0
输出：true
解释：可以找出 (i, j) = (0, 3) 。
满足下述 3 个条件：
i != j --> 0 != 3
abs(i - j) <= indexDiff --> abs(0 - 3) <= 3
abs(nums[i] - nums[j]) <= valueDiff --> abs(1 - 1) <= 0
示例 2：

输入：nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3
输出：false
解释：尝试所有可能的下标对 (i, j) ，均无法满足这 3 个条件，因此返回 false 。
 

提示：

2 <= nums.length <= 105
-109 <= nums[i] <= 109
1 <= indexDiff <= nums.length
0 <= valueDiff <= 109
"""

from typing import List


class Solution:
    def containsNearbyAlmostDuplicate(self, nums: List[int], indexDiff: int, valueDiff: int) -> bool:

        for i,v1 in enumerate(nums):
            for step in range(1,indexDiff+1):
                j=i+step
                if j==len(nums):
                    break
                v2=nums[j]
                if abs(v2 - v1) <= valueDiff:
                    return True

        return False

if __name__=='__main__':
    solution = Solution()
    nums=[1,5,9,1,5,9]
    indexDiff =2
    valueDiff=3
    res=solution.containsNearbyAlmostDuplicate(nums,indexDiff,valueDiff)
    print(res)
    pass